package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.TK;

import com.ym.medical.dao.SymptomDeptDao;
import com.ym.medical.domain.entity.DiseaseDeptEntity;
import com.ym.medical.domain.entity.StandardDeptEntity;
import com.ym.medical.domain.entity.SymptomDeptEntity;
import com.ym.medical.domain.param.SymptomDeptParam;
import com.ym.medical.service.IBaseDataSV;
import com.ym.medical.service.IStandardDeptSV;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class BaseDataSVImpl implements IBaseDataSV {

	@Autowired
	private SymptomDeptDao symptomDeptDao;
	@Autowired
	private IStandardDeptSV standardDeptSV;

	/**
	 * 疾病科室表新增
	 *
	 * @param symptomDept
	 * @return
	 */
	@Override
	public SymptomDeptEntity insertSymptomDept(SymptomDeptParam symptomDept) {
		//为疾病科室设置科室名称 科室编码
		setStandardDept(symptomDept);
		symptomDeptDao.insertSelective(symptomDept.covert());
		return symptomDept.covert();
	}

	/**
	 * 为疾病科室设置科室名称
	 *
	 * @param t
	 */
	@Override
	public <T> void setStandardDept(T t) {
		String deptCode = null;
		if (t instanceof StandardDeptEntity){
			deptCode = ((StandardDeptEntity) t).getDeptCode();
			List<StandardDeptEntity> standardDepts = standardDeptSV.selectList(TK.select(StandardDeptEntity.class)
					.where().andEqualTo(StandardDeptEntity::getDeptCode,deptCode).end());
			if (standardDepts != null && !standardDepts.isEmpty()) {
				((StandardDeptEntity) t).setDeptName(standardDepts.get(0).getDeptName());
			}
		}else if(t instanceof DiseaseDeptEntity){
			deptCode = ((DiseaseDeptEntity) t).getSubDeptCode();
			//根据子科室编码查询科室名称
			List<StandardDeptEntity> subStandardDepts = standardDeptSV.selectList(TK.select(StandardDeptEntity.class)
					.where().andEqualTo(StandardDeptEntity::getDeptCode,deptCode).end());
			if (subStandardDepts != null && !subStandardDepts.isEmpty()) {
				((DiseaseDeptEntity) t).setSubDeptName(subStandardDepts.get(0).getDeptName());
			}
		}

	}

	/**
	 * 疾病科室表修改
	 *
	 * @param symptomDept
	 * @return
	 */
	@Override
	public SymptomDeptEntity updateSymptomDept(SymptomDeptParam symptomDept) {
		SymptomDeptEntity dept = selectSymptomDeptById(symptomDept.getId());
		if (dept == null) {
			throw new BusinessException("该疾病科室不存在");
		}
		setStandardDept(symptomDept);
		symptomDeptDao.updateByPrimaryKeySelective(symptomDept.covert());
		return symptomDept.covert();
	}

	/**
	 * 疾病科室表删除
	 *
	 * @param ids
	 * @return
	 */
	@Override
	public void deleteSymptomDept(List<Long> ids) {
		symptomDeptDao.deleteByExample(TK.select(SymptomDeptEntity.class).where().andIn(SymptomDeptEntity::getId,ids).end());
	}

	/**
	 * 疾病科室表展示
	 *
	 * @param symptomDept
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageInfo<SymptomDeptEntity> listSymptomDept(SymptomDeptParam symptomDept, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<SymptomDeptEntity> symptomDepts = symptomDeptDao.listSymptomDept(symptomDept);
		return new PageInfo<>(symptomDepts);
	}

	/**
	 * 根据id查询
	 *
	 * @param id
	 * @return
	 */
	private SymptomDeptEntity selectSymptomDeptById(Long id) {
		return symptomDeptDao.selectByPrimaryKey(id);
	}
}
